package com.hexu.event.dao;

import com.hexu.event.entity.User;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    //充值
    public boolean addSystemBalance(User user){
        String sql = "update user set system_balance =? where phone_number=?";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setInt(1, user.getSystemBalance());
            ps.setString(2, user.getPhoneNumber());

            ps.executeUpdate();

            //充值成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }

        //充值失败
        return false;
    }

    //返回所有用户信息(密码除外) -->管理员查看
    public List<User> getUserInformation(){
        List<User> list = new ArrayList<>();
        String sql = "select * from user ";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setPhoneNumber(rs.getString("phone_number"));
                user.setUserName(rs.getString("username"));
                user.setSystemBalance(rs.getInt("system_balance"));
                user.setSex(rs.getString("sex"));
                user.setFreeze(rs.getString("freeze"));
                user.setDeblocking(rs.getString("deblocking"));
                list.add(user);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    //返回用户数量
    public int userNumber(){
        String sql = "select phone_number from user ";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            //使游标可滚动
            ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = ps.executeQuery();

            // 将游标移动到最后一行上
            rs.last();
            //获取用户数
            int userCount = rs.getRow();
            //把光标移至第一行前
            rs.beforeFirst();

            return userCount;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return 0;
    }

    //修改用户信息
    public boolean updateUser(User user){
        String sql = "update user set password=?,username=?,sex=? where phone_number=?";

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, JdbcUtil.encrypt(user.getPassword()));
            ps.setString(2,user.getUserName());
            ps.setString(3, user.getSex());
            ps.setString(4, user.getPhoneNumber());
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }

    //读取单个用户信息
    public User getAUser(String phoneNumber,User user){

        String sql = "select * from User where phone_number=?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,phoneNumber);
            rs = ps.executeQuery();
            while (rs.next()){
                user.setId(rs.getInt("id"));
                user.setPhoneNumber(rs.getString("phone_number"));
                user.setPassword(JdbcUtil.decode(rs.getString("password")));
                user.setSystemBalance(rs.getInt("system_balance"));
                user.setSex(rs.getString("sex"));
                user.setUserName(rs.getString("username"));
                user.setFreeze(rs.getString("freeze"));
                user.setDeblocking(rs.getString("deblocking"));
            }
            return user;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    //冻结、解封
    public boolean freeze(User user){
        String sql = "update user set freeze=?,deblocking=? where phone_number=?";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1,user.getFreeze());
            ps.setString(2,user.getDeblocking());
            ps.setString(3, user.getPhoneNumber());

            ps.executeUpdate();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }

        return false;
    }
}
